package com.designMode.interpreter.nonterminal;

import com.designMode.interpreter.AbstractExpression;
import com.designMode.interpreter.Context;
import com.designMode.interpreter.NonterminalExpression;

/**
 * 非运算表达式
 */
public class Not extends NonterminalExpression {
    private AbstractExpression exp;

    public Not(AbstractExpression exp) {
        this.exp = exp;
    }

    @Override
    public Object interpret(Context ctx) {
        Object expObj = exp.interpret(ctx);
        //参数检查
        checkBoolean(expObj, exp.toString());
        return !(Boolean) exp.interpret(ctx);
    }

    @Override
    public boolean equals(AbstractExpression obj) {
        if (obj != null && obj instanceof Not) {
            return exp.equals(((Not) obj).exp);
        }
        return false;
    }

    @Override
    public String toString() {
        return "(NOT " + exp.toString() + ")";
    }
}
